<?php
/**
 * Template Name: Ajax
 */
/**
 * Created by JetBrains PhpStorm.
 * User: Nathan.Nelson
 * Date: 02/12/12
 * Time: 13:11
 * To change this template use File | Settings | File Templates.
 */
$json = array();

$action = $_POST['action'];
$json['action'] = $action;

switch ($action) {
    case 'user_exists':
        $json['performed'] = $action;
        $user_email = $_POST['form_email'];
        $info = (username_exists($user_email) || email_exists($user_email));
    break;

    case 'register':
        $json['performed'] = $action;
        $user_name = $_POST['username'];
        $user_email = $_POST['email'];
        $user_password = $_POST['password'];
        $user_first_name = $_POST['first-name'];
        $user_last_name = $_POST['last-name'];

        $user_id = username_exists($user_name);
        if (!$user_id and email_exists($user_email) == false) {
            if ($user_password == "") {
                $user_password = wp_generate_password($length = 8, $include_standard_special_chars = false);
            }

            $user_id = wp_create_user($user_name, $user_password, $user_email);
            $info = 'Created User <br>';
            $info .= 'Username :' . $user_name . '<br>';
            $info .= 'Email :' . $user_email . '<br>';
            $info .= 'Password :' . $user_password . '<br>';
            $info .= 'First Name :' . $user_first_name . '<br>';
            $info .= 'Last Name :' . $user_last_name . '<br>';

            $user_info = get_userdata($user_id);
            $user_info->remove_role('subscriber');
            $user_info->add_role('customer');

            update_user_meta($user_id, 'first_name', $user_first_name);
            update_user_meta($user_id, 'last_name', $user_last_name);


        } else {
            $info = 'User already exists';
        }
        break;
    case 'book':
        $json['performed'] = $action;

        $slotid = $_POST['slotid'];
        $pitchid = $_POST['pitchid'];
        $venueid = $_POST['venueid'];
        $start_time = $_POST['start-time'];
        $end_time = $_POST['end-time'];
        $day = $_POST['day'];
        $date = $_POST['date'];

        $post = array(
          'comment_status' => 'closed', // 'closed' means no comments.
          'ping_status'    => 'closed', // 'closed' means pingbacks or trackbacks turned off
          'post_status'    => 'publish',
          'post_type'      => 'bookings'
        );

        $new_booking_id = wp_insert_post( $post, $wp_error );

        if ($new_booking_id != 0) {
            update_post_meta($new_booking_id, 'slot', array($_POST['slotid']));
            update_post_meta($new_booking_id, 'date', $_POST['date']);
            update_post_meta($new_booking_id, 'customer_id', $_POST['userid']);

            update_field('field_50b59c4fcfa2c', array($_POST['slotid']), $new_booking_id);
            //update_field('date', $_POST['date'], $new_booking_id);
            //update_field('customer_id', $_POST['userid'], $new_booking_id);
            //update_field($field_key, $value, $post_id);

            $date = DateTime::createFromFormat('Ymd', $_POST['date']);
            if (isset($date)) {
                $name = $date->format('l, d M Y');
            }

            $posts = get_field('slot', $slotid);
            $name = $name . ' - ' . get_the_title($slotid);

            $where = array('ID' => $new_booking_id);
            $slug = sanitize_title( $name );
            $wpdb->update($wpdb->posts, array('post_name' => $slug), $where);
            $wpdb->update($wpdb->posts, array('post_title' => $name), $where);

            //my_acf_save_post( $new_booking_id );


            $json['info'] = 'added booking!';
            $json['url'] = '/thanks/';
        } else {

        }

        $json['post'] = $_POST;
        $json['wp_error'] = $wp_error;
        $json['new_booking_id'] = $new_booking_id;
        break;
    case 'get_date_slots':
        $json['performed'] = $action;

        $slot_date = $_POST['date'];
        $slot_day = $_POST['date'];

        break;
    default:
        $json['performed'] = "No Action Performed";
}
$json['info'] = $info;
echo json_encode($json);